package com.library.controller;

import com.library.bean.User;
import com.library.service.UserService;
import com.library.service.impl.UserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Map;

/**
 * @Author:max
 * @Date: 2024/9/14 16:06
 * @Description:
 */

@WebServlet("/registr")
public class registrServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        UserService us = new UserServiceImpl();
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        String realname = req.getParameter("realname");
        Map<String, Object> map = us.userLogin(username, password);
        User user=new User(username,password,realname);
        int num=us.userRegister(user);
        if(num==1){
            req.getSession().setAttribute("msg","注册成功");
            req.getRequestDispatcher("login.jsp").forward(req,resp);
        }else{
            req.setAttribute("sms","注册失败");
            req.getRequestDispatcher("registr.jsp").forward(req,resp);
        }
    }
}
